package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.StoreDetailBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.StoreDetailMapper;
import com.molichuxing.services.infrastructure.dto.request.create.StoreDetailCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.StoreDetailModifyDto;
import com.molichuxing.services.infrastructure.dto.response.StoreDetailDto;
import com.molichuxing.services.infrastructure.service.StoreDetailService;
import com.molichuxing.services.property.StoreDetailFenceShapeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 门店详情表 服务实现类
 * </p>
 *
 * @author zph
 * @since 2019-08-12
 */
@Service("storeDetailService")
public class StoreDetailServiceImpl implements StoreDetailService {


    @Autowired
    StoreDetailMapper storeDetailMapper;

    /**
     * 根据门店id获取门店详情集合
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Integer, StoreDetailDto> getStoreDetailMapByIds(List<Integer> ids) throws Exception {
        List<StoreDetailBean> storeDetailList = storeDetailMapper.getBatchByIds(ids);
        return Convert.toStoreDetailDtoMap(storeDetailList);
    }

    /**
     * 根据门店id查询门店详情
     *
     * @param id
     * @return
     */
    @Override
    public StoreDetailDto getStoreDetailById(Integer id) throws Exception {
        StoreDetailBean storeDetail = storeDetailMapper.getById(id);
        return Convert.toStoreDetailDto(storeDetail);
    }

    /**
     * 添加门店详情
     *
     * @param storeDetailCreateDto
     * @return
     */
    @Override
    public Integer createStoreDetail(StoreDetailCreateDto storeDetailCreateDto) throws Exception {
        Assert.notNull(storeDetailCreateDto, "门店添加失败");
        StoreDetailBean bean = Convert.toStoreDetail(storeDetailCreateDto);
        storeDetailMapper.create(bean);
        return bean.getStoreId();
    }

    /**
     * 修改门店详情
     *
     * @param storeDetailModifyDto
     * @return
     */
    @Override
    public Boolean modifyStoreDetail(StoreDetailModifyDto storeDetailModifyDto) throws Exception {
        Assert.notNull(storeDetailModifyDto, "门店不存在");
        Integer storeId = storeDetailModifyDto.getStoreId();
        Assert.notNull(storeId, "门店不存在");
        StoreDetailBean storeDetailBean = storeDetailMapper.getById(storeId);
        Assert.notNull(storeDetailBean, "门店不存在");

        Convert.toStoreDetail(storeDetailModifyDto, storeDetailBean);
        return (storeDetailMapper.modifyById(storeDetailBean) > 0) ? true : false;
    }

}
